<template>
    <div class="home" ref="homeRef">
    <van-nav-bar title="我的实体卡" left-text="" left-arrow @click-left="onClickLeft" :fixed="true" :placeholder="true" />
    <div class="tabs">
        <van-tabs v-model:active="active" @click-tab="changeTabs" :swipeable="true" :ellipsis="false" shrink>
            <van-tab :title="item.name" v-for="item in typeList" :name="item.id"></van-tab>
            <!-- <van-tab title="一次性卡"></van-tab>
            <van-tab title="其他卡"></van-tab> -->
        </van-tabs>
        <div class="sumTitle"  v-if="list.length!=0">
            <title-item title1st="总计" :subTitle="tolal"> </title-item>
        </div>
    </div>
    <div class="content">
        <div v-for="(i,index) in list" :key="index">
            <div class="item">
                <div class="left">
                    <van-icon :name="getAssetURL('order/cardNum.png')" color="#2CB9A4" size="24" class="mar-rig" />
                    <text class="useFunc">卡号：{{ i.exchange_number }}</text>
                </div>
                <div>
                    卡密：{{ i.code }}
                </div>
                <div>
                    {{ i.status == 1 ? '已激活' : i.status == 2 ? '已失效' : '待激活' }}
                </div>
            </div>
            <BorderDashed />
        </div>
        <noData :list="list" :lastPage="last_page" :page="page" />
    </div>
</div>
</template>
<script setup>
import { getAssetURL } from "@/utils/load_assets.js"
import BorderDashed from '@/components/border-dashed/border-dashed.vue'
import TitleItem from "@/components/title-item/title-item.vue";
import { useRouter } from 'vue-router'
import useScroll from '@/hooks/useScroll'
import noData from "@/components/noData/noData.vue"
import { getEntityName, entityList } from "@/services/modules/card";
const router = useRouter();
import { ref ,watch} from 'vue';
import { onMounted } from "vue";
const active = ref(0);
const list = ref([])
const last_page = ref(1)
const page = ref(1)
const typeList = ref([])
const typeId = ref('')
const tolal = ref('')
// 返回按钮
const onClickLeft = () => {
    router.back();
};
const changeTabs =(e)=>{
    console.log(e)
    typeId.value = e.name//将id赋值
    list.value = []//清空数组
    page.value = 1
    getList()
}
const getName = async()=>{
    let res = await getEntityName({type:''})
    typeList.value =  res.data
    list.value = []//清空数组
    if(typeList.value.length === 0){
        return;
    }
    typeId.value = typeList.value[0].id
    if(typeList.value.length&&typeList.value.length!=0){
        getList()
    }
}
// 监听滚动到底部
const homeRef = ref()
const { isReachBottom, scrollTop } = useScroll(homeRef)
watch(isReachBottom, (newValue) => {
    if (newValue) {
        if (page.value == last_page.value) {
            return
        } else {
            page.value++
            getList()
        }
    }
})
//获取列表
const getList = async () => {
    let res = await entityList({ page: page.value, type: '' ,id:typeId.value})
    list.value = [...list.value, ...res.data.data]
    tolal.value = res.data.total
    last_page.value = res.data.last_page
}
onMounted(()=>{
    getName()
})
</script>
<style lang="scss" scoped>
::v-deep .van-tabs__line {
    background: #218CC9 !important;
}

::v-deep .van-tabs {
    // padding-right: 80px;
}

::v-deep .van-tab--active {
    color: #2CB9A4 !important;
}
.home {
    background: #F6F6F6;
    height: 100vh;
    overflow-y: auto;
    box-sizing: border-box;
}
.tabs {
    position: sticky !important;
    top: 46px !important;
    z-index: 10 !important;
    background: #fff;

    .sumTitle {
        margin-left: -4px;
        margin-top: 10px;
        padding-right: 20px;
    }
}


.content {
    padding: 20px;

    .item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 12px;
        color: #1D1B20;

        .left {
            display: flex;
            align-items: center;
        }
    }
}</style>